﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeMilkerV2.Repositories;
using System.IO;
using System.CodeDom.Compiler;


namespace CodeMilkerV2 {
    public partial class Form1 : Form {
        private SourceCode _sourceCode;

        public Form1() {
            InitializeComponent();
            _sourceCode = new SourceCode();
        }

        // 选择文件
        private void btnBrowse_Click(object sender, EventArgs e) {
            var dialog = new OpenFileDialog();
            dialog.Filter = "All|*.*|Excel文件|*.xlsx";
            dialog.RestoreDirectory = true;
            dialog.FilterIndex = 2;

            if (dialog.ShowDialog() == DialogResult.OK) {
                txtSource.Text = dialog.FileName;
                this.Text += string.Format(" - {0}", dialog.SafeFileName);
            }
        }

        // 生成代码
        private void btnGenerate_Click(object sender, EventArgs e) {
            if (String.IsNullOrWhiteSpace(txtSource.Text))
                MessageBox.Show("Please Select Source!");

            if (String.IsNullOrWhiteSpace(cboTarget.Text))
                MessageBox.Show("Please select related target!", "Warning");


            // 依据目标，生成代码内容
            var filePath = txtSource.Text;
            TargetEnum target = (TargetEnum)cboTarget.SelectedIndex;

            this._sourceCode = CodeMilker.Mike(filePath, target);
            ntxtCodes.Text = this._sourceCode.FileContent;
            // 提示目标和文件名称
            lblTarget.Text = string.Format("[{0}] -> {1}", target.ToString(), this._sourceCode.FileName);

        }

        // 复制源码
        private void btnCopy_Click(object sender, EventArgs e) {
            if (String.IsNullOrWhiteSpace(ntxtCodes.Text)) {
                MessageBox.Show("No Content!");
                return;
            }
            Clipboard.SetDataObject(ntxtCodes.Text);
        }

        // 保存源码
        private void btnSaveAs_Click(object sender, EventArgs e) {
            if (String.IsNullOrWhiteSpace(ntxtCodes.Text)) {
                MessageBox.Show("No Content!");
                return;
            }

            var dialog = new SaveFileDialog();
            dialog.Filter = this._sourceCode.Ext;
            dialog.FileName = this._sourceCode.FileName;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK) {
                using (StreamWriter sw = new StreamWriter(dialog.FileName, false)) {
                    sw.Write(ntxtCodes.Text);
                }
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e) {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            txtSource.Text = path;



        }
        private void Form1_DragEnter(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                e.Effect = DragDropEffects.Link;
            }
            else {
                e.Effect = DragDropEffects.None;
            }

        }


    }
}
